package com.greenline.push.home.controllers.domain;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.greenline.guser.annotation.GuserResource;
import com.greenline.push.common.json.BaseJson;
import com.greenline.push.common.json.GridJsonData;
import com.greenline.push.core.pushcenter.DomainManager;
import com.greenline.push.dal.pushcenter.dataobject.DomainDO;
import com.greenline.push.dal.query.DomainQuery;

@Controller
@RequestMapping(value = "/domain")
public class DomainController {

    @Autowired
    private DomainManager domainManager;
    private final Log logger = LogFactory.getLog(DomainController.class);

    @GuserResource("模板域配置列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String queryRecord(ModelMap model) {
        return "domain/index";
    }

    @GuserResource("模板域配置列表")
    @RequestMapping("/query.json")
    @ResponseBody
    public GridJsonData<DomainDO> listRecord(DomainQuery query,
            @RequestParam(value = "page", defaultValue = "1", required = false) String pageIndex,
            @RequestParam(value = "pageSize", defaultValue = "10", required = false) String pageSize) {
        query.setPageIndex(Integer.parseInt(pageIndex));
        query.setPageSize(Integer.parseInt(pageSize));
        GridJsonData<DomainDO> ret = new GridJsonData<DomainDO>();
        ret.setQuery(domainManager.listByQuery(query));
        return ret;
    }

    @GuserResource("模板域配置列表")
    @RequestMapping(value = "/listall-domain", method = RequestMethod.GET)
    public String listallDomain(ModelMap model) {
        return "domain/listall-domain";
    }

    @GuserResource("模板域配置添加")
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String addInit(ModelMap model) {
        return "domain/add";
    }

    @GuserResource("模板域配置添加")
    @RequestMapping(value = "/add.json", method = RequestMethod.POST)
    public @ResponseBody
    BaseJson add(ModelMap model, @ModelAttribute("domain") DomainDO domain) {
        BaseJson json = new BaseJson();
        try {
            DomainDO tmp = domainManager.getByCode(domain.getCode());

            if (tmp != null) {
                json.setHasError(true);
                json.setMessage("提交出错,code重复");
                return json;
            }

            if (domainManager.insert(domain) > 0) {
                json.setMessage("提交成功！");
            } else {
                json.setHasError(true);
                json.setMessage("提交出错");
            }
        } catch (Exception e) {
            json.setHasError(true);
            json.setMessage("提交出错");
            logger.error(json.getMessage(), e);
        }
        return json;
    }

    @GuserResource("模板域配置修改")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String updateInit(ModelMap model, @PathVariable Long id) {
        DomainDO object = domainManager.getDomainById(id);
        model.put("domain", object);
        return "domain/update";
    }

    @GuserResource("模板域配置修改")
    @RequestMapping(value = "/update.json", method = RequestMethod.POST)
    public @ResponseBody
    BaseJson update(ModelMap model, @ModelAttribute("domain") DomainDO domain) {
        BaseJson json = new BaseJson();
        try {
            DomainDO tmp = domainManager.getByCode(domain.getCode());
            // cann't set to other domain code.
            if (tmp != null) {
                domain.setCode(null);
            }

            if (domainManager.update(domain)) {
                json.setMessage("提交成功！");
            } else {
                json.setHasError(true);
                json.setMessage("提交出错");
            }
        } catch (Exception e) {
            json.setHasError(true);
            json.setMessage("提交出错");
            logger.error(json.getMessage(), e);
        }
        return json;
    }

    @GuserResource("模板域配置删除")
    @RequestMapping(value = "/delete.json", method = RequestMethod.POST)
    public @ResponseBody
    BaseJson delete(ModelMap model, @ModelAttribute("id") Long id) {
        BaseJson json = new BaseJson();
        try {
            if (domainManager.delete(id)) {
                json.setMessage("删除成功！");
            } else {
                json.setHasError(true);
                json.setMessage("删除出错");
            }
        } catch (Exception e) {
            json.setHasError(true);
            json.setMessage("删除出错");
            logger.error(json.getMessage(), e);
        }
        return json;
    }

}
